﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoreData;
using ActionServices;
using Helper;

namespace ZidoniCMS.Controllers
{
    //trừ trường hợp bất khả kháng không thể xử lý ở mức service mới được phép xử lý trên controller
    public class ArticleController : BaseController
    {
        //
        // GET: /Article/
        public ArticleController(IArticleActionService _article, IUserActionService _user, IConfigurationActionService _config, ICategoryArticleActionService _category)
            : base(_article, _user,_config, _category)
        { }

        /// <summary>
        /// View All Article
        /// </summary>
        /// <returns></returns>
        public ActionResult ListArticle(int page=1, int PageSize=12)
        {
            try
            {
                var article = ArticleService.GetListByLINQ(x=>x.Active==true).ToList();

                if (article != null && article.Count() > 0)
                {
                    foreach (var item in article)
                    {
                        if (item.Image == "" || FileHelper.FileExits(Server.MapPath("/Media/Article/"), item.Image) == false)
                        {
                            item.Image = "no-images.png";
                        }
                    }
                    ViewBag.page = page;
                    ViewBag.PageSize = PageSize;
                    ViewBag.TotalPage = (int)Math.Ceiling((float)article.Count / (float)PageSize);
                    article = article.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                }
                return View(article);
            }
            catch
            {
                return View();
            }
        }


        public ActionResult ShowByCateID(int page = 1, int pageSize = 12, int Id = 0, FormCollection frm = null)
        {
            if (Id == 0)
                return View("Error");
            var cat = CategoryArticleService.GetByID(Id);
            if (cat == null)
                return View("Error");
            List<Article> model = ArticleService.GetListByRelationsID(Id).ToList();
            if (model.Count > 0)
            {
                ViewBag.page = page;
                ViewBag.PageSize = pageSize;
                ViewBag.TotalPage = (int)Math.Ceiling((float)model.Count / (float)pageSize);
                model = model.OrderByDescending(x => x.ID).Skip((page - 1) * pageSize).Take(pageSize).ToList();

                ViewBag.Title = cat.Name;
                ViewBag.CatId = cat.ID;
                ViewBag.Link = cat.Link;
            }
            return View(model);
        }
      

        /// <summary>
        /// View Detail 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult Details(long Id)
        {
            var data = ArticleService.GetByID(Id);
            if (data == null)
                data = new Article();
            return View(data);
        }
        /// <summary>
        /// Bài viết liên quan
        /// </summary>
        /// <param name="id"></param>
        /// <param name="catid"></param>
        /// <returns></returns>
        public ActionResult _ListRelationArticle(int id, int catid) {
            var data = ArticleService.GetListByLINQ(x => x.Active == true && x.CateID == catid && x.ID != id).Take(10).ToList();
            if (data!=null&&data.Count() > 0)
            {
                foreach (var item in data)
                {
                    if (item.Image == "" || FileHelper.FileExits(Server.MapPath("/Media/Article/"), item.Image) == false)
                    {
                        item.Image = "no-images.png";
                    }
                }
            }
            return PartialView(data);
        }
        /// <summary>
        /// List Article o trang chu
        /// </summary>
        /// <returns></returns>
        public ActionResult _ListArticleOnHome() {
            var data = ArticleService.GetListByLINQ(x=>x.Active && x.ShowHomePage==true).ToList();
            return PartialView(data);
        }
    }
}
